﻿using FileReceiveWeb.Models;
using NLog;
using System;
using System.Web;
using System.Web.Http;

namespace FileReceiveWeb.Controllers
{
    public class GemstarFileController: ApiController
    {
        public static readonly Logger log = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 接收.3传来的文件 保存到问吧文件夹
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string PostSaveFile()
        {
            var request = HttpContext.Current.Request;
            var fileCollect = request.Files;
            var fileSize = request.Headers.Get("FileSize");
            string result = "error";
            try
            {
                foreach (string item in fileCollect.AllKeys)
                {
                    HttpPostedFile file = fileCollect[item];
                    string fileName = file.FileName;
                    file.SaveAs(AppSettingConstant.saveLocal + file.FileName);
                    log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}：保存文件{file.FileName}成功");
                    result = "ok";
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:{result}");
            }
            return result;
        }
    }
}